package com.atguigu.gulimail.order.config;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import com.atguigu.gulimail.order.vo.PayVo;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

@ConfigurationProperties(prefix = "alipay")
@Component
@Data
public class AlipayTemplate {

    //在支付宝创建的应用的id
    private   String app_id = "2021000122600462";

    // 商户私钥，您的PKCS8格式RSA2私钥
    private  String merchant_private_key = "MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCIzLB0SYchgCORjJwrWmK/Hpxd6qvjIPjbzFwzuYJw/PksqbNxdxjCcwOJJ92et79eLX+Hdi69cxG+rh0sHrU7zLR3Hp1++mzRhIISE0dfHubisIQ2tyqQpGWVrKP40yj/TGbzBHELF8n8zzidB+eFuJ0FAGY0g3z4Ayr/n4QZhIQHvbpyfDW7zn9ZdfY/dBtR22pIdtGACVMB0l7LZBea51W07RPOA4ZvxiSXSE0bKOr6zt0isLmaOC8Ol/oFOf+7p5qX162+dZnFLqtbyyYfNnIiuznZKB2P1xBtJCH5eN6f3D0CUnqDYXVlkqrcdfAWrSlh+1J870C2SlxSuUjBAgMBAAECggEATEQZh+tKK/IdYlLK6hyM+tIdCPsA8zJ5q0iGisu8njNa03yUrwhfxGCMsxWHp6n3RZEGDUZlDneth+nYF7XTAlDAt7xYv+brbe96+IrI+o3m2LDiNQLu9w7AF67VB67pp1M3/foW78FGeewpOtqiR9nBqH0tfc9kh3x0p2iOJA71LnB3JEwsk9kP9bHGT9vF6eX1WoFbEIlGljjoZ798xZJdWCUutPSn9D2TqFM5A77MVMoP79k2MeoO4JF9CwTyfCkbFEfIyErg0xDl4h6CIGxKCpUPB+QzjVAc+OGNvY8gs2f3820DTCnYN9tX1Qbi3wBodr4Kp8S2PrxLnDAZtQKBgQD3CGvYYY1GBpPgur8E15Z+xqfpYk2f7VewLu3I4mEMBhTkQ99ugXZFV0+8hvxlAJHJ7vP4mb5DLt44qR55Xqo5desP3S5fpBcN7EAmy/bQqdXRsTbsN6lg1yQSSQ/UcU4cZUPZWb8i2Czxh2HIgFOXz2cpVeKamZ54rtLcwwDfuwKBgQCNw+nUv454bW9WrY5zqvz+4jXCA970a5LcuQrH+YjBZu9U+GHLw6eEAJSQSD65kPfVZiOoe2b++FKF34GFU9W196E8S2sn+Ppvaj5SxA3JQ5Xt/krqOLsWwnf+xYWIN+Z/pgI6elw8zeMEypi5YfrJS6vvlnPHvsYJYjEAo/p7swKBgAr74t+yJrfwXu7lHs02UMi+SIUGC/GKehWpYIVIG4yPUKp9ohelxf57TV5SmjJbfh/+cfahz2XpVZQrYOGApgz8A0GnUgKKp0PFN20CMK1Cf/Fc8dY6+1sE9eOiQAPwLcrxbbziUq9X8Vej72Mc4ZqkbC/YU8UQA43WKCYMvaM1AoGARqkyMgLQ5qHVbyTO7kR6GilyliQLy1SbPT1WbFqqmYvPG1E6nbAGHTuU0V3RNflWLp3chCwOKGRp7aR8EJK64Rpu2XjWDnSBj2sraWGjZYA0iQ3JFXn1duCzot+dMUp7r4B1d+RpeT0+PLMm8LAvqon3R1J8n0EtwJuaxTD2txUCgYB8r6lpGsc5TESnmiNdoU/iI4kOisHw6wZ4l2vYLoGkynY93rocLJVNpzS09d00RAZkruE6fy9CIdKcFKeqmm6E9isODXHBFuvDr8qcbybZCUHeh2JFi87i8K6jjCYZV3xzJe959jtWLfodkQ1k07ELxLaiouyGLEuMzOMZqY71qQ==";
    // 支付宝公钥,查看地址：https://openhome.alipay.com/platform/keyManage.htm 对应APPID下的支付宝公钥。
    private  String alipay_public_key = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAnHrp2VRQMlVXup4w7oLGCfGlU1OEuLM6MuxNFwT7LpcIlU8/7h/8mLftk3QrOuz7LjXR25JFjEzhEqvTAtbTcA/QFsWl9fUqFw9wDIhZDZqz9pIiqVPCKEOTiHLkFxLRKllf2/vInsc7Bw1juTjUbppi75x1ATlwPt/7jxUFX8FjxtlsHsdkQGyq1TI24LlIF4fGpkeVu/q10lVTKzHFr40fuLfrz0QNJWd6gPIjGSE2MtoNGEBI6Et2e2zo6F/EJoP359E535Vs1HfzLEebXSanWfOzeV8OrYtGQbbNpd1mzcE7DcDTj77Qv5wmOps1N90+b3mzdAlI7MOVacHrVQIDAQAB";
    // 服务器[异步通知]页面路径  需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    // 支付宝会悄悄的给我们发送一个请求，告诉我们支付成功的信息
    private  String notify_url;

    // 页面跳转同步通知页面路径 需http://格式的完整路径，不能加?id=123这类自定义参数，必须外网可以正常访问
    //同步通知，支付成功，一般跳转到成功页
    private  String return_url = "http://127.0.0.1:8000/memberOrder.html";

    // 签名方式
    private  String sign_type = "RSA2";

    // 字符编码格式
    private  String charset = "utf-8";

    // 支付宝网关； https://openapi.alipaydev.com/gateway.do
    private  String gatewayUrl = "https://openapi.alipaydev.com/gateway.do";

    public  String pay(PayVo vo) throws AlipayApiException {

        //AlipayClient alipayClient = new DefaultAlipayClient(AlipayTemplate.gatewayUrl, AlipayTemplate.app_id, AlipayTemplate.merchant_private_key, "json", AlipayTemplate.charset, AlipayTemplate.alipay_public_key, AlipayTemplate.sign_type);
        //1、根据支付宝的配置生成一个支付客户端
        AlipayClient alipayClient = new DefaultAlipayClient(gatewayUrl,
                app_id, merchant_private_key, "json",
                charset, alipay_public_key, sign_type);

        //2、创建一个支付请求 //设置请求参数
        AlipayTradePagePayRequest alipayRequest = new AlipayTradePagePayRequest();
        alipayRequest.setReturnUrl(return_url);
        alipayRequest.setNotifyUrl(notify_url);

        //商户订单号，商户网站订单系统中唯一订单号，必填
        String out_trade_no = vo.getOut_trade_no();
        //付款金额，必填
        String total_amount = vo.getTotal_amount();
        //订单名称，必填
        String subject = vo.getSubject();
        //商品描述，可空
        String body = vo.getBody();

        alipayRequest.setBizContent("{\"out_trade_no\":\""+ out_trade_no +"\","
                + "\"total_amount\":\""+ total_amount +"\","
                + "\"subject\":\""+ subject +"\","
                + "\"body\":\""+ body +"\","
                + "\"timeout_express\":\"5m\"," //订单超时时间
                + "\"product_code\":\"FAST_INSTANT_TRADE_PAY\"}");

        String result = alipayClient.pageExecute(alipayRequest).getBody();

        //会收到支付宝的响应，响应的是一个页面，只要浏览器显示这个页面，就会自动来到支付宝的收银台页面
        System.out.println("支付宝的响应："+result);

        return result;

    }
}
